package com.google.fbyoutube.client.page;

import java.util.HashMap;
import java.util.Map;

import com.google.fbyoutube.client.YouTube;
import com.google.fbyoutube.client.YouTubeAppService;
import com.google.fbyoutube.client.YouTubeAppServiceAsync;
import com.google.fbyoutube.client.widget.LoadingPopup;
import com.google.fbyoutube.client.widget.PalHeader;
import com.google.fbyoutube.client.widget.PalWatchVideo;
import com.google.fbyoutube.client.widget.Tabhome;
import com.google.fbyoutube.shared.FBUtility;
import com.google.fbyoutube.shared.FeedUrlMaker;
import com.google.fbyoutube.shared.ytVideoEntry;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtfb.sdk.FBCore;
import com.gwtfb.sdk.FBXfbml;

public class Home extends Composite {

	public static YouTubeAppServiceAsync videoService = GWT
			.create(YouTubeAppService.class);

	public FBCore fbCore = GWT.create(FBCore.class);

	private LoadingPopup lp = new LoadingPopup();

	private boolean status = true;
	private boolean xfbml = true;
	private boolean cookie = true;
	private String channelUrl = FBUtility.APP_CALLBACK_URL + "channel.html";
	private String required_perm = "email, user_status, user_videos, friends_videos";

	public Home() {

		VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSize("720px", "615px");

		PalHeader widHeader = new PalHeader();
		verticalPanel.add(widHeader);
		verticalPanel.setCellHeight(widHeader, "90px");

		TabBar tabBar = new TabBar();
//		TabPanel tabPanel = new TabPanel();
//		tabPanel.setAnimationEnabled(true);
//		verticalPanel.add(tabPanel);
//		tabPanel.setSize("100%", "100%");
		verticalPanel.add(tabBar);

		panelSwitcher = new SimplePanel();
		panelSwitcher.setStylePrimaryName("mainframe");
		tabBar.addTab("Home");
		tabBar.addTab("Invite Friends");
//		tabPanel.add(panelSwitcher, "Watch Videos", false);
		verticalPanel.add(panelSwitcher);
		videoList = new Tabhome();
		panelSwitcher.setWidget(videoList);

//		tabPanel.selectTab(0);
		tabBar.selectTab(0);
		tabBar.addSelectionHandler(
				new SelectionHandler<Integer>() {

					@Override
					public void onSelection(SelectionEvent<Integer> event) {
						if (event.getSelectedItem() == 0
								&& panelSwitcher.getWidget() instanceof PalWatchVideo) {
							// [Watch Video] tab selected
							showVideoList();
						} else if (event.getSelectedItem() == 1) {
							// [Invite friends] tab selected
							JSONObject data = new JSONObject();
							data.put("method", new JSONString("apprequests"));
							data.put(
									"message",
									new JSONString(
											"Hi, I invite you to join Youtube on Facebook!"));

							YouTube.getAppGUI().fbCore.ui(
									data.getJavaScriptObject(),
									new AsyncCallback<JavaScriptObject>() {

										@Override
										public void onSuccess(
												JavaScriptObject result) {
											// TODO(xiaodao): Auto-generated
											// method stub

										}

										@Override
										public void onFailure(Throwable caught) {
											// TODO(xiaodao): Auto-generated
											// method stub

										}
									});
						}

					}
				});
		if (Window.Location.getParameter(FBUtility.VIDEO_ID_PARAM) != null) {
			videoService.getVideoById(
					Window.Location.getParameter(FBUtility.VIDEO_ID_PARAM),
					new AsyncCallback<ytVideoEntry>() {

						@Override
						public void onSuccess(ytVideoEntry result) {
							watchVideo(result);
						}

						@Override
						public void onFailure(Throwable caught) {
							loadDefaultVideos();
							// Failed to find given video, show video list
							// instead
						}
					});
		} else {
			loadDefaultVideos();
		}

		// Facebook init
		fbCore.init(FBUtility.APPID, channelUrl, status, cookie, xfbml);
		fbCore.setAutoGrow();
	}

	public void resetVideoList(String sortBy, String cat, String region,
			Map<String, String> options) {
		loadingData();
		if (sortBy == null) {	// Search feed
			this.videoList.enableVideoPaging(true);
		} else {
			this.videoList.enableVideoPaging(false);
		}
		this.videoList.loadVideos(FeedUrlMaker.buildUrl(sortBy, cat, region, options));
	}

	public void watchVideo(ytVideoEntry video) {
		loadingData();
		if (panelSwitcher.getWidget() instanceof Tabhome) {
			videoList = (Tabhome) panelSwitcher.getWidget();
		}
		PalWatchVideo watchVideo = new PalWatchVideo(video);
		panelSwitcher.setWidget(watchVideo);
		FBXfbml.parse();
		complelteLoading();
	}

	public void showVideoList() {
		if (panelSwitcher.getWidget() instanceof PalWatchVideo) {
			watchVideo = (PalWatchVideo) panelSwitcher.getWidget();
		}
		panelSwitcher.setWidget(videoList);
		if (!videoList.isLoadComplte()) {
			loadDefaultVideos();
		}
	}

	private void loadDefaultVideos() {
		Map<String, String> options = new HashMap<String, String>();
		options.put(FeedUrlMaker.TIME, FeedUrlMaker.DefaultDueTime);
		resetVideoList(FeedUrlMaker.MOST_POPULAR_FEED, FeedUrlMaker.DEFAULT_CAT, null, options);
	}

	public void loadingData() {
			lp.show();
	}

	public void complelteLoading() {
			lp.hide();
	}

	private SimplePanel panelSwitcher = null;
	private Tabhome videoList = null;
	private PalWatchVideo watchVideo = null;

}
